package com.wind.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wind.annotation.PermInfo;
import com.wind.constant.Root;
import com.wind.entity.Announcement;
import com.wind.entity.SysUser;
import com.wind.service.IAnnouncementService;
import com.wind.util.PageUtils;
import com.wind.vo.Json;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/wind_ann")
public class AnnouncementController {
    private static final Logger log = LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    private IAnnouncementService announcementService;
    @PostMapping
    public Json add(@RequestBody String body) {

        String oper = "add ann";
        log.info("{}, body: {}",oper,body);

        Announcement ann = JSON.parseObject(body, Announcement.class);

        if (StringUtils.isEmpty(ann.getTitle())) {
            return Json.fail(oper, "活动公告名不能为空");
        }
        if (StringUtils.isEmpty(ann.getImage())) {
            return Json.fail(oper, "图片不能为空");
        }
        if (StringUtils.isEmpty(ann.getContent())){
            return Json.fail(oper, "图文详情不能为空");
        }
        Announcement annDB = announcementService.selectOne(new EntityWrapper<Announcement>().eq("title", ann.getTitle()));
        if (annDB != null) {
            return Json.fail(oper, "公告已发布");
        }

        //保存新用户数据
        ann.setUpdateTime(new Date());
        ann.setCreateTime(new Date());

        boolean success = announcementService.insert(ann);
        return Json.result(oper, success)
                .data("id",ann.getId())
                .data("createTime",ann.getCreateTime());
    }

    @PermInfo("删除活动公告")
    @DeleteMapping
    public Json delete(@RequestBody String body) {

        String oper = "delete ann";
        log.info("{}, body: {}",oper,body);
        Announcement ann = JSON.parseObject(body, Announcement.class);
        ann.setIsDelete(1);
        boolean success = announcementService.updateById(ann);
        return Json.result(oper, success).data("isDelete",ann.getIsDelete());
    }

    @PostMapping("/query")
    public Json query(@RequestBody String body) {
        String oper = "query ann";
        log.info("{}, body: {}", oper, body);
        JSONObject json = JSON.parseObject(body);
        String name = json.getString("title");
        Wrapper<Announcement> wrapper=new EntityWrapper<>();
        wrapper.like("title",name);
        //Page<Announcement> page = announcementService.query(PageUtils.getPageParam(json), name);
        Page<Announcement> page=announcementService.selectPage(new Page<>(1,10),wrapper);
        return Json.succ(oper).data("page", page);
    }


    @PermInfo("更新活动公告的信息")
    @PatchMapping("/info")
    public Json update(@RequestBody String body) {

        String oper = "update ann";
        log.info("{}, body: {}", oper, body);

        Announcement ann = JSON.parseObject(body, Announcement.class);

        ann.setUpdateTime(new Date());

        //boolean success = sysUserService.update(user,new EntityWrapper<User>().eq("uid",user.getUid()));
        boolean success = announcementService.updateById(ann);

        return Json.result(oper, success).data("updateTime",ann.getUpdateTime());
    }

}
